package com.hangxintong.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author zxkj
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Bean
    public HandlerInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(getLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/error")
                .excludePathPatterns("/member/level/getLevelList")
                .excludePathPatterns("/level/getLevelList")
                .excludePathPatterns("/company/editMemberCompany")
                .excludePathPatterns("/message/aliyun/**", "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**")
                .excludePathPatterns("/h5/assoc/**", "/company/getAllList", "/company/getCompanyInfoTwo", "/company/saveCompanyStatus", "/activity/activityClockUser/exportClockUserExcel")
                .excludePathPatterns("/acitvity/**")
                .excludePathPatterns("/companyNew/registCompany")
                .excludePathPatterns("/info/exportActivityUsers")
                .excludePathPatterns("/h5/activity/listActivinfoH5")
                .excludePathPatterns("/activity/h5/activity/listActivinfoH5")
                .excludePathPatterns("/h5/activity/info/**")
                .excludePathPatterns("/activity/h5/activity/info/**")
                //定时任务路径

                .excludePathPatterns("/dues/test","/info/updateInfoStatus","/dues/bill/task/generate","/dues/bill/isGenerated/**")
                .excludePathPatterns("activitySignUser/updateSignUserOrderStatus")
                .excludePathPatterns("/activitySignUser/updateSignUserOrderStatus")
                .excludePathPatterns("/activity/activitySignUser/updateSignUserOrderStatus");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html","/webjars/**","/swagger-ui.html","/static/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/")
                .addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .maxAge(3600)
                .allowedHeaders("*");
    }



}
